package com.ruoyi.animal.service.impl;

import java.util.List;

import com.ruoyi.animal.domain.AnimalInformation;
import com.ruoyi.animal.mapper.AnimalInformationMapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.animal.mapper.HealthCheckupMapper;
import com.ruoyi.animal.domain.HealthCheckup;
import com.ruoyi.animal.service.IHealthCheckupService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 动物健康检测Service业务层处理
 * 
 * @author gw
 * @date 2025-01-06
 */
@Service
public class HealthCheckupServiceImpl implements IHealthCheckupService 
{
    @Autowired
    private HealthCheckupMapper healthCheckupMapper;

    @Autowired
    private AnimalInformationMapper animalInformationMapper;

    /**
     * 查询动物健康检测
     * 
     * @param checkupId 动物健康检测主键
     * @return 动物健康检测
     */
    @Override
    public HealthCheckup selectHealthCheckupByCheckupId(Long checkupId)
    {
        return healthCheckupMapper.selectHealthCheckupByCheckupId(checkupId);
    }

    /**
     * 查询动物健康检测列表
     * 
     * @param healthCheckup 动物健康检测
     * @return 动物健康检测
     */
    @Override
    public List<HealthCheckup> selectHealthCheckupList(HealthCheckup healthCheckup)
    {
        return healthCheckupMapper.selectHealthCheckupList(healthCheckup);
    }

    /**
     * 新增动物健康检测
     * 
     * @param healthCheckup 动物健康检测
     * @return 结果
     */
    @Override
    @Transactional
    public int insertHealthCheckup(HealthCheckup healthCheckup)
    {
        healthCheckup.setCreateTime(DateUtils.getNowDate());
        healthCheckup.setCreateBy(SecurityUtils.getUsername());
        AnimalInformation animalInformation = animalInformationMapper.selectAnimalInformationByAnimalId(healthCheckup.getAnimalId());
        if(animalInformation!=null){
            animalInformation.setLastCheckupDate(healthCheckup.getCheckupDate());
            animalInformation.setHealthStatus(healthCheckup.getHealthStatus());
            animalInformationMapper.updateAnimalInformation(animalInformation);
        }
        return healthCheckupMapper.insertHealthCheckup(healthCheckup);
    }

    /**
     * 修改动物健康检测
     * 
     * @param healthCheckup 动物健康检测
     * @return 结果
     */
    @Override
    public int updateHealthCheckup(HealthCheckup healthCheckup)
    {
        healthCheckup.setUpdateTime(DateUtils.getNowDate());
        healthCheckup.setUpdateBy(SecurityUtils.getUsername());
        AnimalInformation animalInformation = animalInformationMapper.selectAnimalInformationByAnimalId(healthCheckup.getAnimalId());
        if(animalInformation!=null){
            animalInformation.setLastCheckupDate(healthCheckup.getCheckupDate());
            animalInformation.setHealthStatus(healthCheckup.getHealthStatus());
            animalInformationMapper.updateAnimalInformation(animalInformation);
        }
        return healthCheckupMapper.updateHealthCheckup(healthCheckup);
    }

    /**
     * 批量删除动物健康检测
     * 
     * @param checkupIds 需要删除的动物健康检测主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteHealthCheckupByCheckupIds(Long[] checkupIds)
    {
        int deletedCount = 0;
        for (Long checkupId : checkupIds) {
            HealthCheckup healthCheckup = healthCheckupMapper.selectHealthCheckupByCheckupId(checkupId);
            healthCheckup.setDeleteBy(SecurityUtils.getUsername());
            healthCheckup.setDeleteTime(DateUtils.getNowDate());
            healthCheckup.setDelFlag(1);
            healthCheckupMapper.updateHealthCheckup(healthCheckup);
            deletedCount++;
        }
        return deletedCount;
    }

    /**
     * 删除动物健康检测信息
     * 
     * @param checkupId 动物健康检测主键
     * @return 结果
     */
    @Override
    public int deleteHealthCheckupByCheckupId(Long checkupId)
    {
        HealthCheckup healthCheckup = healthCheckupMapper.selectHealthCheckupByCheckupId(checkupId);
        healthCheckup.setDeleteBy(SecurityUtils.getUsername());
        healthCheckup.setDeleteTime(DateUtils.getNowDate());
        healthCheckup.setDelFlag(1);
        return healthCheckupMapper.updateHealthCheckup(healthCheckup);
    }
}
